package com.zsyh.jiujiu.fragment.magager;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.zyh.toolslib.base.BaseFragment;
import com.example.zyh.toolslib.utils.SPUtils;
import com.google.gson.Gson;
import com.lwb.retrofit.RetrofitTask;
import com.zsyh.jiujiu.Api;
import com.zsyh.jiujiu.R;
import com.zsyh.jiujiu.bean.MessageEvn;
import com.zsyh.jiujiu.bean.MyGoodBean;
import com.zsyh.jiujiu.net.MyFragementObserver;
import com.zsyh.jiujiu.utils.BigDecimalUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @ProjectName: livvehelp
 * @Package: com.zsyh.lsb.ui
 * @ClassName:
 * @Description: java类作用描述
 * @Author: zsyh
 * @CreateDate: 2020/9/7 11:59
 * @UpdateUser: liangwanbo
 * @UpdateDate: 2020/9/7 11:59
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class xiajiaFragment extends BaseFragment {

    @BindView(R.id.xiajialist)
    RecyclerView xiajialist;
    private BaseQuickAdapter<MyGoodBean.DataBean, BaseViewHolder> mAdapter;
    List<MyGoodBean.DataBean> list = new ArrayList<>();

    public static xiajiaFragment newInstance(Context contexts, int status) {
        Bundle args = new Bundle();
        args.putInt("status", status);
        xiajiaFragment fragment = new xiajiaFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int initLayout() {
        return R.layout.fragment_xiajia;
    }

    @Override
    protected void initView(View rootView) {
        EventBus.getDefault().register(this);
        mAdapter = new BaseQuickAdapter<MyGoodBean.DataBean, BaseViewHolder>(R.layout.item_shangjia, list) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, MyGoodBean.DataBean item) {
                String[] split = item.getGoodsPic().split("\\|");
                Glide.with(getActivity()).load(split[0]).into(helper.<ImageView>getView(R.id.huanrulogo));
                helper.setText(R.id.huanrugoodname, item.getGoodsTitle());
                helper.setText(R.id.money, item.getGoodsPrice() + "");
                helper.setText(R.id.kucun, "共" + item.getStockCount() + item.getGoodsUnit() + "商品 总价值：¥" + BigDecimalUtil.decimalTwo(item.getGoodsPrice() * item.getStockCount()));
                helper.setVisible(R.id.bianji, false);
                helper.setText(R.id.xiajia, "上架");
                helper.setOnClickListener(R.id.shanchu, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        shanchu(item.getId());
                    }
                });

                helper.setOnClickListener(R.id.xiajia, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        xiajia(item.getId(), "1");
                    }
                });
            }
        };
        xiajialist.setLayoutManager(new LinearLayoutManager(getActivity()));
        xiajialist.setAdapter(mAdapter);
        MyGood((String) SPUtils.get(getActivity(), "userid", ""), "2");
    }

    @Override
    protected void initData() {


    }


    /**
     * 我的商品
     */
    public void MyGood(String userid, String stattus) {
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .getMyGoods(userid, stattus)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyFragementObserver(getActivity(), true) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("wodexiajia", JsonStr);
                        Gson gson = new Gson();
                        MyGoodBean goodBean = gson.fromJson(JsonStr, MyGoodBean.class);
                        if (list != null) {
                            list.clear();
                        }
                        List<MyGoodBean.DataBean> nlist = new ArrayList<>();
                        for (int i = 0; i < goodBean.getData().size(); i++) {
                            list.add(goodBean.getData().get(i));
                            nlist.add(goodBean.getData().get(i));
                        }
                        mAdapter.notifyDataSetChanged();
                        EventBus.getDefault().post(new MessageEvn(nlist.size() + "", "gaibian"));


                    }
                });

    }

    /**
     * xiajia
     */
    public void xiajia(String goodid, String status) {
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .updateGoodsStatus(goodid, status)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyFragementObserver(getActivity(), false) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("wode", JsonStr);
                        if (list != null) {
                            list.clear();
                        }
                        mAdapter.notifyDataSetChanged();
                        MyGood((String) SPUtils.get(getActivity(), "userid", ""), "2");
                    }
                });


    }

    /**
     * 删除
     */
    public void shanchu(String doodid) {
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .delGoods(doodid)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyFragementObserver(getActivity(), false) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("wode", JsonStr);
                        MyGood((String) SPUtils.get(getActivity(), "userid", ""), "2");
                    }
                });
    }

    @Override
    protected void initOnlyFirstLazy() {
        super.initOnlyFirstLazy();
        MyGood((String) SPUtils.get(getActivity(), "userid", ""),"2");

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(MessageEvn messageInfo) {
        if (messageInfo.getMessage().equals("all")) {
            MyGood((String) SPUtils.get(getActivity(), "userid", ""),"2");
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

}
